var personCtrl  = new XFApp.client();
$.views.converters({
	subString : function(val){
		return personCtrl.subString(val,7);
	},
	
	ftime : function (val){		
		return personCtrl.formateTime(val);
	},

    formatDate : function(val){
    	var date = new Date(val);
    	return date.format("yyyy-MM-dd hh:mm:ss");
    },
	
	fauditStatus : function(value) {
		if(!value){
			return "";
		}
		for(index in auditStates){
			var auditState = auditStates[index];
			if(auditState.dictCode == value){
				return auditState.dictName;
			}
		}
		return  "未知类型("+value+")";
	},
	
	factiveStatus : function(value) {
		if(!value){
			return "";
		}
		for(index in activeStates){
			var activeState = activeStates[index];
			if(activeState.dictCode == value){
				return activeState.dictName;
			}
		}
		return  "未知类型("+value+")";
	},
	
	
});


XFApp.client.prototype.person = {
		successCode : "10000",	
		personPage : null,
		checkVal :"",
		showId : null,
		registerType : registerType,
		urlItems : {
			initListURL: basePath+"/registerManager/init",
			approveURL : basePath+"/registerManager/approve",
			resetPwdURL : basePath+"/registerManager/resetPwd",
			showDetailURL:basePath+"/registerManager/showDetail",
		},	
		
		//显示详细
		showDetail : function(id){
			_this = this;
			_this.showId = id ;
			_this.detail(id);			
		},
		
		detail : function (id){			
			_this = this;		
			var params = {					
					id : id 
			};
			
			$.ajax ({
				url :_this.urlItems.showDetailURL,
				data : params,
				dataType : "json",
				type : 'post',
				success : function(data) {
					if(data.code == _this.successCode){						
						$("#register_info_modal").modal();
						$("#dt_userId").text(data.result.userId);
						$("#dt_realName").text(data.result.realName);
						$("#email").text(data.result.email);
						$("#mobile").text(data.result.mobile);
						$("#reason").val(data.result.reason);
		        	}else{
		        		
		        		window.webDialogUtil.alert("提示",data.msg);		        		
		        	}
				}
			});			
		},
		
		
		
		load : function (){
			
            _this = this;
            
            var audit = $("#auditState").attr("data-id"); 
            //3：查询全部
            if(audit == '3')audit = "";        
			
			var registerInfoVo = {				
					userId : $("#userId").val(),
					realName : $("#realName").val(),
					auditState : audit ,
					activeState :"1",
					registerType : _this.registerType
			}
			
			if(!!_this.personPage){
				_this.personPage.page("remote",0,registerInfoVo);
			}else{
				_this.personPage = $("#page").page({
				pageSize : 10,
				remote: {
					url: _this.urlItems.initListURL,
			        params: registerInfoVo, 
			        success: function (data, pageIndex) {
			        	if(data.code==_this.successCode){			        		
			        		if(null == data 
									|| null == data.result 
										|| null == data.result.itemList
											|| data.result.itemList.length < 1){
			        			$("#data_grid").html('<tr><td align="center" colspan="11">暂无数据</td></tr>');
			        		}else{
			        			$("#data_grid").html($("#dataTmp").render(data.result.itemList));
			        		}			        		
			        	}else{
			        		window.webDialogUtil.alert("提示","数据查询失败!");			        		   
			        	}
			        },
			        error : function(){
			        	
			        	window.webDialogUtil.alert("提示","数据查询失败!");			      	  
			        }
				},
				firstBtnText : '首页',
				lastBtnText : '尾页',
				prevBtnText : '上一页',
				nextBtnText : '下一页',
				showInfo : true,
				showJump : true,
				jumpBtnText : '跳转',
				showPageSizes : true,
				infoFormat : '共{pageCount}页'
			});	
		   }			
		},
		
		initSelect : function (){
			
			$("#auditState").bsSuggest({
				idField: "dictCode",  
				keyField: "dictName",
				effectiveFields :["dictCode","dictName"],
				effectiveFieldsAlias:{dictCode:"编码",dictName:"名称"},
		        data: {
		           'value':auditStates
		        }
		   });
			
		},
		
		approve : function (option,type){
			
            _this = this;
            
            if(!_this.checkVal){
            	
            	window.webDialogUtil.alert("提示","请选择一条记录进行操作!!");
            	return ;
            }
            
            webAlert({
                title:'提示',
                content:'确认执行'+type+'操作吗?',
                opacity:.5,
                bg:"gray",
                width:300,
                height:10,
                button:[
                        {
                        name:"确定",
                        callback:function(){
                        	_this.approveConfirm(option);
                        	this.close();
                        }
                        },{
                        name:"取消",
                        callback:function(){
                      	  this.close();
                        }
                        }]
                });
            
            
		},		
		
	
	  	approveConfirm: function(option){
	  	    	
	  	    	var index = _this.checkVal.lastIndexOf(",");            
	            var id = _this.checkVal.substring(0,index);
	            
	            var showText ;
	            if(option == '1')
	            	showText = '审批通过';
	            else
	            	showText = '审批拒绝';
	            
	            var params = {	
	             		op  : option, //1-审批通过 2-审批拒绝
	             		ids : id 				
	 			}
	  	    	
		    	$.ajax({
		    		url :_this.urlItems.approveURL,
		    		data : params,
		    		dataType : "json",
		    		type : 'post',
		    		success : function(data) {				
		    			if(data.code==_this.successCode){
		    				
		    				window.webDialogUtil.alert("提示",data.msg);					
					
		    				$("[name='chkItem']:checked").each(function (){
		    					$("#ad"+$(this).val()).text(showText);
		    				})
					
		    				//移除事件
		    				$("#checkAll").removeAttr("checked");
		    				$("[name='chkItem']:checkbox").removeAttr("checked");//取消全选
					
					    	        		
		    			}else{
		    				
		    				window.webDialogUtil.alert("提示",data.msg);
		    		}
		    	}
		    });
		 },
		
		//重置密码
        resetPwd : function (type){
			
            _this = this;
            
            if(!_this.checkVal){
            	
            	window.webDialogUtil.alert("提示","请选择一条记录进行操作!!");            	
            	return ;
            }
            
            webAlert({
                title:'提示',
                content:'确认执行'+type+'操作吗?',
                opacity:.5,
                bg:"gray",
                width:300,
                height:10,
                button:[
                        {
                        name:"确定",
                        callback:function(){
                        	_this.resetPwdConfirm();
                        	this.close();
                        }
                        },{
                        name:"取消",
                        callback:function(){
                      	  this.close();
                        }
                        }]
            });
        },
       
	  	  resetPwdConfirm: function(){
	  		  	var index = _this.checkVal.lastIndexOf(",");            
	  		  	var id = _this.checkVal.substring(0,index);
	  		  	var params = {	
	        		ids : id 				
	  		  	}
	  		  	
		    	$.ajax({            
		    		url :_this.urlItems.resetPwdURL,
		    		data : params,
		    		dataType : "json",
		    		type : 'post',
		    		success : function(data) {				
		    			if(data.code==_this.successCode){
		    				
		    				window.webDialogUtil.alert("提示",data.msg);		    				
					
		    				//移除事件
		    				$("#checkAll").removeAttr("checked");
		    				$("[name='chkItem']:checkbox").removeAttr("checked");//取消全选
					
					    	        		
		    			}else{
		    				
		    				window.webDialogUtil.alert("提示",data.msg);
		    			}
		    		}
		    	});
	  	  },
        
        
        detailApprove : function (option,type){
			
            _this = this;
            
            var params = {	
            		op  : option, //1-审批通过 2-审批拒绝
            		ids : _this.showId 				
			}
            
            
            webAlert({
                title:'提示',
                content:'确认执行'+type+'操作吗?',
                opacity:.5,
                bg:"gray",
                width:300,
                height:10,
                button:[
                        {
                        name:"确定",
                        callback:function(){
                        	_this.detailApproveConfirm(params);
                        	this.close();
                        }
                        },{
                        name:"取消",
                        callback:function(){
                      	  this.close();
                        }
                        }]
            });
        },
            
   
        detailApproveConfirm: function(params){
    		    	$.ajax({
    		    		url :_this.urlItems.approveURL,
    		    		data : params,
    		    		dataType : "json",
    		    		type : 'post',
    		    		success : function(data) {				
    		    			if(data.code==_this.successCode){
    		    				
    		    				$("#register_info_modal").modal('hide');    		    				
    		    				$("#ad"+_this.showId).text(data.msg);
    		    				
    		    			}else{
    		    				
    		    				window.webDialogUtil.alert("提示",data.msg);	
    		    			}
    		    		}
    		    	});
    	},
    	  
		
		
		event : function(){
			_this = this;
			
			$("#btnSearch").on("click",function(){
				_this.load();
			});
			
			$("#btnReset").on("click",function(){
				$("#userId").val("");
				$("#realName").val("");
				$("#auditState").val("");
				$("#auditState").attr("data-id","");
			});
			//审批通过
			$("#btnPass").on("click",function(){
				var chkVal = "";
				$("#data_grid input[type='checkbox']").each(function(){
					if($(this).prop("checked")){
						chkVal += $(this).val() +",";
					}
				});		
				
				_this.checkVal = chkVal ;				
				_this.approve(1,$("#btnPass").text());
			});
			//审批拒绝
			$("#btnReject").on("click",function(){
				var chkVal = "";
				$("#data_grid input[type='checkbox']").each(function(){
					if($(this).prop("checked")){						
						chkVal += $(this).val()+",";						
					}
				});
				_this.checkVal = chkVal ;
				_this.approve(2,$("#btnReject").text());
			});
			
			//重置密码
			$("#btnResetPwd").on("click",function(){
				var chkVal = "";
				$("#data_grid input[type='checkbox']").each(function(){
					if($(this).prop("checked")){						
						chkVal += $(this).val()+",";						
					}
				});
				_this.checkVal = chkVal ;
				_this.resetPwd($("#btnResetPwd").text());
			});
			
			$("#xClose").on("click",function(){
				$("#register_info_modal").hide();
			});
			$("#btnClose").on("click",function(){
				$("#register_info_modal").hide();
			});
			
			
			$("#approve").on("click",function(){
				_this.detailApprove(1,$("#approve").text());
			});
			
			$("#reject").on("click",function(){
				if(!$("#reason").val()){					
					window.webDialogUtil.alert("提示","[拒绝理由]不能为空,请填写");
					return ;
				}
				_this.detailApprove(2,$("#reject").text());
			});
			
			$("#checkAll").bind("click",function(){				
			   var chkVal = ""; 
			   //全选
			   if($("#checkAll").prop("checked")){
				   $("[name='chkItem']").prop("checked","checked");
				   $("[name='chkItem']:checkbox:checked").each(function (){
					   chkVal += $(this).val()+",";
				   })
				   
			   }else{
				   $("[name='chkItem']").removeAttr("checked");//取消全选
				   chkVal = "";
			   }	
			   _this.checkVal = chkVal;
			});	
			
		},
		
		
};


$(function(){
	personCtrl.person.load();
	personCtrl.person.event();
	personCtrl.person.initSelect();
});